﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entities;
using DTOs;
using System.IO;
using System.Configuration;

namespace giftdeliveryservice.Controllers.Admin
{
    public class ProductAdminController : Controller
    {
        //
        // GET: /ProductAdmin/

        public ActionResult Add()
        {
            DataManager manager = new DataManager();
            var cats = manager.GetProductCategories();
            ViewBag.Categories = new SelectList(cats, "Id", "Name", cats[0]);

            return View();
        }


        [HttpPost]
        public ActionResult Add(ProductDTO product, HttpPostedFileBase imageFile)
        {

            //you can put your existing save code here
            if (imageFile != null && imageFile.ContentLength > 0)
            {
                //save the file 
                var fileName = Path.GetFileName(imageFile.FileName);
                var path = Path.Combine(Server.MapPath(ConfigurationManager.AppSettings["PRODUCTIMAGEFOLDER"]), fileName);
                imageFile.SaveAs(path);

                product.Image = fileName;
            }

            DataManager manager = new DataManager();
            manager.AddProduct(product);

            var cats = manager.GetProductCategories();
            ViewBag.Categories = new SelectList(cats, "Id", "Name", cats[0]);
            return View();
        }

    }
}
